from osgeo import ogr

Shapefile = "D:/Code/Python/pyGDAL/Include/dataset/points.shp"

dataSource:ogr.DataSource = ogr.Open(Shapefile,1)
layer:ogr.Layer = dataSource.GetLayer(0)
layerdefn:ogr.FieldDefn = layer.GetLayerDefn()

for index in range(layerdefn.GetFieldCount()):
    fieldName =  layerdefn.GetFieldDefn(index).GetName()
    fieldTypeCode = layerdefn.GetFieldDefn(index).GetType()
    fieldType = layerdefn.GetFieldDefn(index).GetFieldTypeName(fieldTypeCode)
    fieldWidth = layerdefn.GetFieldDefn(index).GetWidth()
    precision = layerdefn.GetFieldDefn(index).GetPrecision()
    print(fieldName + "--" + fieldType + "--" + str(fieldWidth) +"--" + str(precision))

print("----------------------------------------------")

for feature in layer:
    feature: ogr.Feature
    id = feature.GetFieldAsString("Id")
    name = feature.GetFieldAsString("name")
    disc = feature.GetFieldAsString("disc")
    geom:ogr.Geometry = feature.GetGeometryRef()
    x = geom.GetX()
    y = geom.GetY()
    print("%s-%s-%s-%f-%f"%(id,name,disc,x,y))

# 删除要素
# layer.SetAttributeFilter("name='FF'")
# for feature in layer:
#     feature:ogr.Feature
#     # feature.SetField("name","FF") #SetField 新增修改
#     # layer.CreateFeature(feature)
#     layer.DeleteFeature(feature.GetFID())

#变更要素属性值
layer.SetAttributeFilter("name='EE'")
for feature in layer:
    feature:ogr.Feature
    feature.SetField2("name","FF") # SetField2 替换修改
    layer.SetFeature(feature)

dataSource.Destroy()


